﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class Demo01 : System.Web.UI.Page
    {
            SqlHelper helper = new SqlHelper();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGV();
                }
            }
        private void BindGV()
        {
            string sql = "select * from StudentInfo";
            DataSet ds = helper.Get(sql, null);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataKeyNames = new string[] { "stu_id" };
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
            {
                string name = TextBox1.Text;
                string sql = "select * from StudentInfo where stu_name like '%'+@name+'%'";
                SqlParameter[] pars =
                {
                new SqlParameter("name",name)
            };

            DataSet ln = helper.Get(sql, pars);
            GridView1.DataSource = ln.Tables[0];
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            string email = TextBox3.Text;
            string intro = TextBox4.Text;

            string sql = "insert into StudentInfo (stu_name,password,email,intro) values (@name,@pwd,@email,@intro)";
            SqlParameter[] pars =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro)
            };

            int result = helper.Set(sql, pars);
            if (result != -1)
            {
                BindGV();
            }
            else
            {
                Response.Write("注册失败！");
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGV();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGV();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //传递当前行的索引 获取主键的值 
            string stu_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete from StudentInfo where stu_id=@stu_id";
            SqlParameter[] pars = {
                new SqlParameter("stu_id",stu_id)
            };
            int result = helper.Set(sql, pars);
            if (result != -1)
            {
                BindGV();
            }
            else
            {
                Response.Write("删除失败！");
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            /*
             * GridView1. Rows[e.RowIndex]. Cells[0].Controls[0]
             * GridView1下的正在被编辑的行中的单元格中的第一个对象
             * 
             * as TextBox
             * 转换为TextBox控件
             * 
             * 控件内显示的文本
             * .Text
             * 
             * (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text
             * 获取被编辑的‘行>单元格>控件’的文本
             */
            string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text;
            string pwd = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
            string email = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
            string intro = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;

            string sql = "update  StudentInfo set stu_name=@name,password=@pwd,email=@email,intro=@intro where stu_id=@id";
            SqlParameter[] pars =
           {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro),
                new SqlParameter("@id",id)
            };
            int result = helper.Set(sql, pars);
            if (result != -1)
            {
                GridView1.EditIndex = -1;
                BindGV();
            }
            else
            {
                Response.Write("修改失败！");
            }
        }
    }
    }